package com.bnzj.cloud.business.core.service;

import com.bnzj.cloud.business.core.persistence.entity.GroupCourse;
import com.bnzj.cloud.business.core.persistence.repository.GroupCourseRepository;
import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.rest.CurrentUserService;
import com.bnzj.core.rest.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

@Service
public class GroupCourseService extends BaseService<GroupCourseRepository, GroupCourse,Long> {

    @Autowired
    private GroupCourseRepository groupCourseRepository;

    @Autowired
    private CurrentUserService currentUserService;

    @Transactional
    public boolean groupAssignCourse(String groupId, String courseIds) {
        String[] courseIdArr = courseIds.split(",");
        UserInfo userInfo = currentUserService.getUserInfo();

        if(courseIdArr.length > 0) {
            groupCourseRepository.deleteByGroupId(groupId);
            for(String str : courseIdArr) {
                GroupCourse groupCourse = new GroupCourse();
                groupCourse.setGroupId(Long.parseLong(groupId));
                groupCourse.setCourseId(Long.parseLong(str));
                groupCourse.setCreator(userInfo.getAccount());
                groupCourse.setCreatorName(userInfo.getName());
                groupCourse.setCreateTime(new Date());
                groupCourseRepository.newSave(groupCourse);
            }
            return true;
        } else {
            return false;
        }
    }

}
